<?
include_once "db.php";
include_once "users.php";
include_once "session.php";

class Messages
{
	function Messages() {
		global $configuration;
		$this->service = $configuration->get("service");
		$this->domain = $configuration->get("domain");
	}
	function newMessage($to, $from, $title, $text, $submitedBy) {
		global $db;
		global $session;
		global $users;
		$u = $users->getUserByName($to);
		if ($u) {
			$db->query("INSERT INTO", "messages", "(msg_to, msg_from, title, message, submitedBy, date) VALUES  ('".quote_smart($u)."', '".quote_smart($from)."', '".quote_smart($title)."', '".quote_smart($text)."', '".quote_smart($submitedBy)."', '".date("Y-m-d H:i:s")."')");
			return 1;
		}/*
		if (!(strpos($to, "@") === false)) {
			$domain = substr($to, strpos($to, "@")+1);
			if ($session->logedin)
				if ($domain != $this->domain) {
					return mail($to, $title, $text, 'From: '.$from."\r\n");
				}
			$to = substr($to, 0, strpos($to, "@"));
		}
		if (is_numeric($to)) {
			$db->query("INSERT INTO", "messages", "(msg_to, msg_from, title, text, submitedBy, date) VALUES  ('".quote_smart($to)."', '".quote_smart($from)."', '".quote_smart($title)."', '".quote_smart($text)."', '".quote_smart($submitedBy)."', '".date("Y-m-d H:i:s")."')");
			return 1;
		} else {
			return 0;
		}*/
	}
	function getMyMessages($limit = false, $since = false) {
		global $db;
		global $session;
		if (!$limit) {
			$data = $db->queryToArray("SELECT * FROM", "messages", " WHERE msg_to = ".$session->user." ORDER BY date ", true);
		} else {
			$data = $db->queryToArray("SELECT * FROM", "messages", " WHERE msg_to = ".$session->user." ORDER BY date LIMIT ".$limit, true);		
		}
		if (is_array($data)) {
			return $data;
		} else {
			return false;
		}
	}

}

$messages = new Messages();

?>